/*
 * SPDX-FileCopyrightText: 2025 LakeSoul Contributors
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package com.dmetasoul.e2e;

import com.dmetasoul.lakesoul.tables.LakeSoulTable;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;

/**
 * @author mag1cian
 */
public class Main {
  public static void main(String[] args) {
    var spark =
        SparkSession.builder()
            .appName("{{appName}}")
            .getOrCreate();
    var targetSchema = spark.table("{{tableName}}").schema();
    var df1 = spark.read().parquet("{{dataPath}}");
    var df2 = spark.createDataFrame(df1.rdd(), targetSchema);

    {%- if hasPrimaryKey -%}

    LakeSoulTable.forName("{{tableName}}").upsert(df2,"");

    {%- else -%}

    df2.write().mode("append").format("lakesoul").saveAsTable("{{tableName}}");

    {%- endif -%}

    spark.stop();
  }
}
